import request from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderInfo: {},
    orderStatus: '',
    orderId: 0,
    showModal: false,
    modalGoodsInfo: {},
    showRefundModal: false,
    refundData: {}
  },

  /**
   * 去商品页面
   */
  toGoods(options) {
    let goodsId = options.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/goodsPackage/pages/goods/goods?goodsId=' + goodsId,
    })
  },

  /**
   * 退款按钮
   */
  async applyRefundButton(options) {
    this.refundData = options
    this.setData({
      showRefundModal: true,
      refundData: options
    })
  },

  inputChange(options) {
    this.refundData.remark = options.detail.value;
  },

  onConfirm() {
    this.applyRefund();
  },

  onCancel() {
    this.refundData = {}
    this.setData({
      showRefundModal: false
    })
  },

  /**
   * 退款按钮处理
   */
  async applyRefund(options) {
    let orderSn = this.refundData.currentTarget.dataset.orderSn;
    let orderGoodsSn = this.refundData.currentTarget.dataset.orderGoodsSn;
    let remark = this.refundData.remark;
    let result = await request("/order/applyRefund", {
      orderSn,
      orderGoodsSn,
      remark
    }, 'POST')
    if (result.code == 200) {
      let options = {
        orderId: this.data.orderId
      }
      this.onLoad(options)
      wx.showToast({
        title: '申请退款成功'
      })
      this.refundData = {}
      this.setData({
        showRefundModal: false
      })
    }
  },

  /**
   * 查看退款详情按钮
   */
  checkRefundRecord(options) {
    let modalGoodsInfo = this.data.orderInfo.orderGoodsDetailDtoList[options.currentTarget.dataset.index]
    this.setData({
      showModal: true,
      modalGoodsInfo
    })
  },

  /**
   * 模态框关闭按钮
   */
  onClose() {
    this.setData({
      showModal: false,
      modalGoodsInfo: {}
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function(options) {
    let orderId = options.orderId
    let result = await request("/order/detail", {
      orderId
    })

    let orderStatus = result.data.orderStatus
    switch (orderStatus) {
      case 0:
        orderStatus = '待支付'
        break;
      case 1:
        orderStatus = '已支付，待发货'
        break;
      case 2:
        orderStatus = '已支付，已发货'
        break;
      case 3:
        orderStatus = '已取消'
        break;
      case 4:
        orderStatus = '商家已取消'
        break;
      case 5:
        orderStatus = '部分商品申请退款/部分商品取消或退款'
        break;
      case 6:
        orderStatus = '已退款'
        break;
    }
    this.setData({
      orderInfo: result.data,
      orderStatus,
      orderId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})